            try {
                // 确保地址格式正确
                const registrar = web3.utils.toChecksumAddress(config.registrar);
                const student = web3.utils.toChecksumAddress(config.student);
                const contractAddress = web3.utils.toChecksumAddress(config.contractAddress);
                
                // 确保 chainId 是数字
                const chainId = parseInt(config.chainId);

                // 构建要签名的消息
                const message = web3.utils.soliditySha3(
                    { t: 'uint256', v: chainId },
                    { t: 'address', v: contractAddress },
                    { t: 'string', v: "REGISTRAR_SIG" },
                    { t: 'address', v: registrar },
                    { t: 'address', v: student },
                    { t: 'string', v: config.semester },
                    { t: 'string', v: config.cid }
                );

                console.log("消息哈希:", message);

                // 使用 personal_sign 进行签名
                const signature = await window.ethereum.request({
                    method: 'personal_sign',
                    params: [message, currentAccount]
                });

                console.log("签名结果:", signature);

                // 显示结果
                document.getElementById('resultArea').innerHTML = `
                    <h3>签名结果：</h3>
                    <p>消息哈希：${message}</p>
                    <p>签名数据：${signature}</p>
                    <button onclick="navigator.clipboard.writeText('${signature}')">复制签名</button>
                `;

            } catch (error) {
                console.error("签名生成失败:", error);
                alert(`签名失败: ${error.message}`);
            } 